package view;

import java.awt.DisplayMode;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

public class Screen {
	/* Reference to the graphics device */
	private GraphicsDevice display;
	
	/** Constructs a new screen */
	public Screen() {
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		this.display = ge.getDefaultScreenDevice();
	}
	
	/** Returns the height of window */
	public int getHeight() {
		Window window = this.display.getFullScreenWindow();
		if (window != null) {
			return window.getHeight();
		} else {
			return 0;
		}
	}
	
	/** Returns the width of the window */
	public int getWidth() {
		Window window = this.display.getFullScreenWindow();
		if (window != null) {
			return window.getWidth();
		} else {
			return 0;
		}
	}
	
	/** Returns a list of available display modes */
	public DisplayMode[] getAvailableDisplayModes () {
		return this.display.getDisplayModes();
	}
	
	/** Returns the current display mode */
	public DisplayMode getCurrentDisplayMode() {
		return this.display.getDisplayMode();
	}
	
	/** Returns the graphics object */
	public Graphics2D getGraphics() {
		// Get full screen window
		Window window = this.display.getFullScreenWindow();
		
		// Get buffer strategy from window and create a graphics object from it
		if (window != null) {
			BufferStrategy strategy = window.getBufferStrategy();
			return (Graphics2D)strategy.getDrawGraphics();
		} else {
			return null;
		}
	}
	
	/** Returns the full screen window */
	public Window getFullScreenWindow() {
		return this.display.getFullScreenWindow();
	}
	
	/** Sets window to full screen */
	public void setFullScreen(DisplayMode displayMode) {
		// Create frame
		JFrame window = new JFrame();
		window.setUndecorated(true);
		window.setIgnoreRepaint(true);
		window.setResizable(false);
		
		// Set the window to full screen
		display.setFullScreenWindow(window);
		
		// Change the display mode
		if (displayMode != null && display.isDisplayChangeSupported()) {
			display.setDisplayMode(displayMode);
		}
		
		// Create buffer strategy
		window.createBufferStrategy(2);
	}
	
	/** Restores the window to normal size */
	public void restoreScreen() {
		Window window = display.getFullScreenWindow();
		if (window != null) {
			window.dispose();
		}
		display.setFullScreenWindow(null);
	}
	
	/** Updates display */
	public void update() {
		// Get full screen window
		Window window = this.display.getFullScreenWindow();
		
		// Show the contents of the window
		if (window != null) {
			BufferStrategy strategy = window.getBufferStrategy();
			if (strategy.contentsLost() != true) {
				strategy.show();
			}
		}
	}
	
	/** Create image compatible with monitor */
	// TODO research use
	public BufferedImage createCompatibleImage(int width, int height, int transparancy) {
		Window window = this.display.getFullScreenWindow();
		
		if (window != null) {
			GraphicsConfiguration gc = window.getGraphicsConfiguration();
			return gc.createCompatibleImage(width, height, transparancy);
		} else {
			return null;
		}
	}
}
